package com.Service.impl;

import com.Service.GroupActivityService;
import com.mapper.GroupActivityMapper;
import com.mapper.GroupJoinMapper;
import com.mapper.ProductMapper;
import com.pojo.GroupActivity;
import com.pojo.GroupActivityVO;
import com.pojo.GroupJoin;
import com.pojo.Product;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class GroupActivityServiceImpl implements GroupActivityService {
    @Resource
    private GroupActivityMapper groupActivityMapper;

    @Override
    public List<GroupActivity> findAll() {
        return groupActivityMapper.findAll();
    }

    @Override
    public GroupActivity findActiveByProductId(Long productId) {
        return groupActivityMapper.selectActiveByProductId(productId, LocalDateTime.now());
    }

    @Override
    public void add(GroupActivity groupActivity) {
        groupActivityMapper.insert(groupActivity);
    }

    @Override
    public GroupActivity findById(Long id) {
        return groupActivityMapper.selectById(id);
    }

    @Override
    public void update(GroupActivity groupActivity) {
        groupActivityMapper.update(groupActivity);
    }

    @Override
    public void delete(Long id) {
        groupActivityMapper.delete(id);
    }

    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<GroupActivityVO> findAllVO() {
        List<GroupActivity> activities = groupActivityMapper.findAll();
        List<GroupActivityVO> voList = new ArrayList<>();

        for (GroupActivity activity : activities) {
            GroupActivityVO vo = new GroupActivityVO();
            BeanUtils.copyProperties(activity, vo);
            vo.setId(activity.getId()); // 补上这行
            Product product = productMapper.findById(activity.getProductId());
            vo.setProduct(product);
            voList.add(vo);
        }

        return voList;
    }

    @Autowired
    private GroupJoinMapper groupJoinMapper;
    @Override
    public GroupActivityVO findVOById(Long id) {
        GroupActivity activity = groupActivityMapper.selectById(id);
        Product product = productMapper.findById(activity.getProductId());

        GroupActivityVO vo = new GroupActivityVO();
        BeanUtils.copyProperties(activity, vo);
        vo.setProduct(product);

        return vo;
    }

    @Override
    public List<GroupActivity> findAllAO() {
        return null;
    }

    @Override
    public Long initiateGroup(Long productId, Long userId) {
        // 从 group_activity 中查询该商品的拼团模板配置（仅查询一条即可）
        GroupActivity template = groupActivityMapper.selectTemplateByProductId(productId);

        if (template == null) {
            throw new RuntimeException("该商品暂不支持拼团，请联系管理员添加拼团配置。");
        }

        // 创建拼团活动（可以复制模板部分内容）
        GroupActivity activity = new GroupActivity();
        activity.setProductId(productId);
        activity.setGroupPrice(template.getGroupPrice()); // ✅ 使用模板中的价格
        activity.setGroupSize(template.getGroupSize());   // ✅ 使用模板中的成团人数
        activity.setStartTime(new Date());

        // 设置 24 小时后结束
        Date endTime = new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000);
        activity.setEndTime(endTime);
        activity.setStatus("1");

        groupActivityMapper.insert(activity);

        // 创建拼团参与记录（团主）
        GroupJoin join = new GroupJoin();
        join.setActivityId(activity.getId());
        join.setUserId(userId);
        join.setJoinedAt(new Date());
        join.setIsInitiator(true); // 团主
        groupJoinMapper.insert(join);

        return activity.getId();
    }

}
